package com.sc.byahi.user.client;

import java.util.Arrays;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.sc.byahi.user.shared.ProfileOwner;

public class UserType extends Composite {

	private static UserTypeUiBinder uiBinder = GWT.create(UserTypeUiBinder.class);

	// private ProfileOwner owner = null;

	interface UserTypeUiBinder extends UiBinder<Widget, UserType> {
	}

	public UserType() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiField
	RadioButton user;
	@UiField
	RadioButton agent;
	@UiField
	TextBox agentName;
	@UiField
	Button ok;

	private void buttonClicked(ClickEvent event) {
		if (!user.getValue() && !agent.getValue()) {
			UIUtil.showDialog(Arrays.asList("Please select any one type of user."), "User type selection");
			return;
		} else if (agent.getValue() && agentName.getText().trim().isEmpty()) {
			UIUtil.showDialog(Arrays.asList("Please enter name of your marriage bureau."), "User type selection");
			return;
		} else {
			ProfileOwner owner = AppController.get().getOwner();
			if (user.getValue()) {
				owner.setIsAgency(false);
			} else if (agent.getValue()) {
				owner.setIsAgency(true);
				owner.setAgencyName(agentName.getText().trim());
			} else {

			}
			// now send this to server backend, add history token after that
			GreetingService.Util.getInstance().updateProfileOwner(AppController.get().getOwner(), new AsyncCallback<ProfileOwner>() {
				@Override
				public void onSuccess(ProfileOwner result) {
					History.newItem("dashboard");
					History.fireCurrentHistoryState();
				}

				@Override
				public void onFailure(Throwable caught) {
					UIUtil.showGenericFailureDialog();
				}
			});
		}
	}

	@UiHandler("ok")
	void onButton_1Click(ClickEvent event) {
		buttonClicked(event);
	}
}
